package com.letme.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.letme.common.utils.SecurityUtils;
import com.letme.common.utils.StringUtils;
import com.letme.store.mapper.MallUserAddressMapper;
import com.letme.store.domain.MallUserAddress;
import com.letme.store.service.IMallUserAddressService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 用户地址Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-18
 */
@Service
public class MallUserAddressServiceImpl extends ServiceImpl<MallUserAddressMapper,MallUserAddress> implements IMallUserAddressService
{
    @Override
    public List<MallUserAddress> queryUserAddress() {
        String openId = SecurityUtils.getOpenId();
        List<MallUserAddress> list = this.baseMapper.selectList(new LambdaQueryWrapper<MallUserAddress>()
                .eq(MallUserAddress::getOpenId, openId).eq(MallUserAddress::getDelFlag, 0));
        return list;
    }

    @Override
    public Boolean updateAddressDefault(Long id) {
        //先把之前的默认地址取消
        String openId = SecurityUtils.getOpenId();
        Boolean flag = this.lambdaUpdate()
                .set(MallUserAddress::getDefaultFlag, "0")
                .eq(MallUserAddress::getOpenId, openId)
                .update();
        if(flag){
            //设置默认地址
            flag = this.lambdaUpdate()
                    .set(MallUserAddress::getDefaultFlag, "1")
                    .eq(MallUserAddress::getId, id)
                    .update();
        }
        return flag;
    }

    @Override
    public Boolean fakeDelAddress(Long id) {
        Boolean flag = this.lambdaUpdate()
                .set(MallUserAddress::getDelFlag, 1)
                .eq(MallUserAddress::getId, id)
                .update();
        return flag;
    }

    @Override
    public Boolean saveAddress(MallUserAddress address) {
        address.setWxUserId(SecurityUtils.getUserId());
        address.setDeptId(SecurityUtils.getDeptId());
        address.setOpenId(SecurityUtils.getOpenId());
        address.setDelFlag(0);
        if(StringUtils.isEmpty(address.getDefaultFlag()) ){
            address.setDefaultFlag("0");
        }
        String userName = SecurityUtils.getWxUser().getWxUserName();
        address.setCreateBy(userName);
        address.setCreateTime(new Date());
        address.setUpdateBy(userName);
        address.setUpdateTime(new Date());
        return this.save(address);
    }

    @Override
    public MallUserAddress queryAddressDetail(Long id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public Boolean updateAddress(MallUserAddress address) {
        address.setUpdateBy(SecurityUtils.getWxUser().getWxUserName());
        address.setUpdateTime(new Date());
        return this.update(new LambdaQueryWrapper<MallUserAddress>().eq(MallUserAddress::getId,address.getId()));
    }
}
